package com.hekang.menudemo;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView tvShow;
    private float fontSize = 20.0f;
    private static final int ITEMID = 4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvShow = (TextView) findViewById(R.id.tv_show);
        tvShow.setTextSize(fontSize);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        //加载XML样式
        getMenuInflater().inflate(R.menu.main, menu);

        //Java代码添加
        menu.add(Menu.NONE, ITEMID, Menu.NONE,"设置字体");

//        MenuItem item =  menu.findItem(R.id.action_color);
//        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                tvShow.setTextColor(Color.RED);
//                return false;
//            }
//        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.action_add:
                tvShow.setTextSize(30.0f);
                break;
            case R.id.action_sub:
                tvShow.setTextSize(10.0f);
                break;
            case R.id.action_reduction:
                tvShow.setTextSize(fontSize);
                break;
            case R.id.action_color:
                tvShow.setTextColor(Color.RED);
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
